package com.base.pig.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.pig.dto.IdDto;
import com.base.pig.dto.PlanListDto;
import com.base.pig.entity.AlarmSetting;
import com.base.pig.entity.WPlan;

import com.base.pig.service.WPlanService;
import com.base.pig.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  巡检计划controller
 * </p>
 *
 * @author wangzhx
 * @since 2022-03-21
 */
@RestController
@Api(value = "WPlanController", tags = "巡检计划管理")
@RequestMapping("/WPlanController")
public class WPlanController {

    @Autowired
    public WPlanService wPlanService;

    @ApiOperation(value = "新增巡检计划")
    @PostMapping("/save")
    public R save(@RequestBody WPlan wPlan){
        long count = wPlanService.count(Wrappers.<WPlan>query().lambda().eq(WPlan::getName,wPlan.getName()));
        if(count == 0) {
            Date date = new Date();
            wPlan.setTime(date);
            boolean b = wPlanService.save(wPlan);
            if (b){
                return R.ok(b,"添加成功");
            }
            return R.failed(null,"添加失败");
        }
        return R.failed(null,"巡检计划名称重复");
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete")
    public R delete(@RequestBody IdDto idDto){
        wPlanService.removeById(idDto.getId());
        return R.ok(null,"删除成功");
    }

    @ApiOperation(value = "获取组织机构树")
    @PostMapping("/queryOrgTree")
    public R queryOrgTree(){
        JSONObject obj = wPlanService.queryOrgTree();
        return R.ok(obj,"获取组织机构树成功");
    }

    /**
     * @param planListDto: 请求参数集合 : {
     *             personid : 巡检人员id
     *             site : 站点
     *             current : 分页数,默认1
     *             size: 每页记录数
     *             }
     * @return R
     * @Title 分页获取巡检计划列表
     * @Description
     * @author wzx
     * @date 2022/3/21
     */
    @ApiOperation(value = "分页获取巡检计划列表")
    @PostMapping("/list")
    public R list(@RequestBody PlanListDto planListDto){
        QueryWrapper planWrapper = new QueryWrapper();
        // 全量查询
        if(StrUtil.isNotBlank(planListDto.getPersonid())){
            planWrapper.eq("personid",planListDto.getPersonid());
        }
        if(StrUtil.isNotBlank(planListDto.getSite())){
            planWrapper.like("site",planListDto.getSite());
        }
        planWrapper.orderByDesc("time");
        IPage<WPlan> page = wPlanService.page(
                new Page<>(planListDto.getCurrent(), planListDto.getSize()),
                planWrapper);
        return R.ok(page,"分页查询信息成功");
    }




    @ApiOperation(value = "根据id修改")
    @PostMapping("/update")
    public R update(@RequestBody WPlan wPlan){
        if(wPlan.getId()!=null)
        {
            long count = wPlanService.count(Wrappers.<WPlan>query().lambda().eq(WPlan::getName,wPlan.getName()).ne(WPlan::getId,wPlan.getId()));
            if(count == 0) {
                Date date = new Date();
                wPlan.setTime(date);
                return R.ok( wPlanService.updateById(wPlan),"修改成功");
            }
            return R.failed(null,"巡检名称重复");
        }
        return R.failed(null,"id不能为空");
    }


}
